﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using QuanLyKho.DKChung;

namespace QuanLyKho.PhongMayKhoan
{
    public partial class CapNhatMayKhoan_Popup : Form
    {
        public static string MaID = "0";
        public static string IdCat = "0";
        public string IdSanPham = "0";
        private static string SoLuongCat = "0";

        public CapNhatMayKhoan_Popup()
        {
            InitializeComponent();
            Loaddata();
            SetPropertiesGrid();
            CheckPyc();
        }

        private void CheckPyc()
        {
            DataTable dtPyc = DataAll.SelectAll("PhieuYeuCau", "ID,LoaiPhieu,TrangThai", "WHERE IdSanPham =" + IdSanPham + " AND PhongBan = " + Convert.ToInt32(PhongBan.PhongMayKhoan));
            if (dtPyc.Rows.Count > 0)
            {
                for (int i = 0; i < dtPyc.Rows.Count; i++)
                {
                    if (dtPyc.Rows[i]["TrangThai"].ToString() == Convert.ToInt32(TrangThai.PhieuYeuCau_CapNhatKho).ToString()
                    || (dtPyc.Rows[i]["TrangThai"].ToString() == Convert.ToInt32(TrangThai.PhieuYeuCau_DuyetPhieu).ToString() &&
                    dtPyc.Rows[i]["LoaiPhieu"].ToString() == Convert.ToInt32(LoaiPhieuYeuCau.HuySanPham).ToString()))
                    {
                        bntLuLai.Visible = true;
                        btnPhieuYeuCau.Visible = true;
                        lblMessage.Visible = false;
                    }
                    else
                    {
                        bntLuLai.Visible = false;
                        btnPhieuYeuCau.Visible = false;
                        lblMessage.Visible = true;
                        lblMessage.Text = "Phiếu yêu cầu trả lại vật tư đang trong quá trình xử lý, không thể cập nhật lệnh.";
                        break;
                    }
                }
            }
            else
            {
                lblMessage.Visible = false;
            }
            if (MaID != "0" && IdCat == "0")
            {
                bntLuLai.Visible = false;
                btnPhieuYeuCau.Visible = false;
            }
        }

        public void Loaddata()
        {
            if (MaID == "0" && IdCat != "0")
            {
                LoadGrid();
                SetPropertiesColumns();
                DataTable dt = DataAll.SelectAll("Cat inner join PhongMau_SanPham on Cat.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],Cat.[ID],Cat.[IdSanPham],Cat.[NgayLapLenh],Cat.[NguoiCapNhatLenh],Cat.[SoLuong],Cat.[NgayGuiLenh],Cat.[NgaySuaLenh],Cat.[TrangThai],Cat.[NgayChungTu],Cat.[NgayHoachToan],Cat.[SoChungTu],Cat.[GhiChu],Cat.[KhoDen],Cat.[KhoDi],Cat.[SXTuNgay],Cat.[SXDenNgay]",
                    "WHERE Cat.ID = " + IdCat);
                dtNgayLapLenh.Value = Convert.ToDateTime(dt.Rows[0]["NgayLapLenh"]);
                txtSoLuong.Text = SoLuongCat = dt.Rows[0]["SoLuong"].ToString();
                txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
                dtNgayChungTu.Value = Convert.ToDateTime(dt.Rows[0]["NgayChungTu"]);
                dtNgayHachToan.Value = Convert.ToDateTime(dt.Rows[0]["NgayHoachToan"]);
                txtChungtu.Text = dt.Rows[0]["SoChungTu"].ToString();
                dtSXTuNgay.Value = Convert.ToDateTime(dt.Rows[0]["SXTuNgay"]);
                dtSXDenNgay.Value = Convert.ToDateTime(dt.Rows[0]["SXDenNgay"]);
                IdSanPham = dt.Rows[0]["IdSanPham"].ToString();
                txtMaSP.Text = dt.Rows[0]["MaSanPham"].ToString();
                txtLoaiHang.Text = dt.Rows[0]["LoaiHang"].ToString();
                txtLanTai.Text = dt.Rows[0]["LanSanXuat"].ToString();
                txtSoLenh.Text = dt.Rows[0]["LenhSanXuat"].ToString();

                DataTable dtPyc = DataAll.SelectAll("PhieuYeuCau", "ID,LoaiPhieu,SoLuong,TrangThai", "WHERE IdSanPham =" + IdSanPham + " AND PhongBan = " + Convert.ToInt32(PhongBan.PhongMayKhoan));
                txtSoLuongMayKhoan.Text = txtSoLuong.Text;
                if (dtPyc.Rows.Count > 0)
                {
                    for (int i = 0; i < dtPyc.Rows.Count; i++)
                    {
                        if (dtPyc.Rows[i]["LoaiPhieu"].ToString() == "3" &&
                        dtPyc.Rows[i]["TrangThai"].ToString() == Convert.ToInt32(TrangThai.PhieuYeuCau_DuyetPhieu).ToString())
                            txtSoLuongMayKhoan.Text = (Convert.ToDouble(txtSoLuongMayKhoan.Text) - Convert.ToDouble(dtPyc.Rows[i]["SoLuong"].ToString())).ToString();
                    }
                }

                var data2 = DataAll.SelectAll(
                    "Cat_ChiTiet ct LEFT JOIN Kho_VatTu vt ON ct.IDVatTu = vt.IDVatTu",
                    "ct.ID,ct.[IDVatTu],vt.MaVatTu as'Mã vật tư',vt.TenVatTu as'Mô tả',[S],[M],[L],[XL]," +
                    "[TongCan] as'Tổng cần', [DMTT] as'ĐMTT',[SoLuongCan] as'SL cần'," +
                    "[DauTam] as'Đầu tấm',ct.[MoTa] as'Mô tả',ct.[GhiChu] as'Ghi chú'",
                    " WHERE IDCat =" + IdCat);
                gridMayKhoan.DataSource = null;
                gridMayKhoan.DataSource = data2;
            }
            else
            {
                SetPropertiesColumns();
                var data = DataAll.SelectAll("GiaCong inner join PhongMau_SanPham on GiaCong.IdSanPham = PhongMau_SanPham.IdSanPham",
                    "PhongMau_SanPham.[MaHang] as MaSanPham,PhongMau_SanPham.[LoaiHang],PhongMau_SanPham.[LanSanXuat],PhongMau_SanPham.[LenhSanXuat],GiaCong.[ID],GiaCong.[IdSanPham],GiaCong.[NgayLapLenh],GiaCong.[NguoiCapNhatLenh],GiaCong.[SoLuong],GiaCong.[NgayGuiLenh],GiaCong.[NgaySuaLenh],GiaCong.[TrangThai],GiaCong.[NgayChungTu],GiaCong.[NgayHoachToan],GiaCong.[SoChungTu],GiaCong.[GhiChu],GiaCong.[KhoDen],GiaCong.[KhoDi],GiaCong.[SXTuNgay],GiaCong.[SXDenNgay]",
                    "WHERE GiaCong.ID =" + MaID);
                dtNgayLapLenh.Value = Convert.ToDateTime(data.Rows[0]["NgayLapLenh"]);
                txtSoLuong.Text = SoLuongCat;
                txtSoLuongMayKhoan.Text = data.Rows[0]["SoLuong"].ToString();
                txtGhiChu.Text = data.Rows[0]["GhiChu"].ToString();
                dtNgayChungTu.Value = Convert.ToDateTime(data.Rows[0]["NgayChungTu"]);
                dtNgayHachToan.Value = Convert.ToDateTime(data.Rows[0]["NgayHoachToan"]);
                txtChungtu.Text = data.Rows[0]["SoChungTu"].ToString();

                IdSanPham = data.Rows[0]["IdSanPham"].ToString();
                txtMaSP.Text = data.Rows[0]["MaSanPham"].ToString();
                txtLoaiHang.Text = data.Rows[0]["LoaiHang"].ToString();
                txtLanTai.Text = data.Rows[0]["LanSanXuat"].ToString();
                txtSoLenh.Text = data.Rows[0]["LenhSanXuat"].ToString();

                var data2 = DataAll.SelectAll(
                    "GiaCong_ChiTiet ct LEFT JOIN Kho_VatTu vt ON ct.IDVatTu = vt.IDVatTu",
                    "ct.ID,ct.[IDGiaCong],ct.[IDVatTu],vt.MaVatTu as'Mã vật tư',[S],[M],[L],[XL]," +
                    "[TongCan] as'Tổng cần', [DMTT] as'ĐMTT',[SoLuongCan] as'SL cần'," +
                    "[DauTam] as'Đầu tấm',ct.[MoTa] as'Mô tả',ct.[GhiChu] as'Ghi chú'",
                    " WHERE ct.IDGiaCong =" + MaID);
                gridMayKhoan.DataSource = null;
                gridMayKhoan.DataSource = data2;
                bntLuLai.Visible = false;
                btnPhieuYeuCau.Visible = false;
            }
            LoadImage();
        }

        public void SetPropertiesGrid()
        {
            gvMayKhoan.OptionsView.ShowGroupPanel = false;
            gvMayKhoan.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            gvMayKhoan.OptionsView.ShowFooter = true;
            for (int i = 0; i < 8; i++)
            {
                gvMayKhoan.Columns[i].OptionsColumn.AllowSort = DefaultBoolean.False;
            }
        }

        public void SetPropertiesColumns()
        {
            //Ma hang
            luMaVatTu.DataSource = DataAll.SelectAll("Kho_VatTu", "IDVatTu,MaVatTu,TenVatTu", "");
            luMaVatTu.Columns.Add(new LookUpColumnInfo("IDVatTu"));
            luMaVatTu.Columns.Add(new LookUpColumnInfo("MaVatTu"));
            luMaVatTu.Columns.Add(new LookUpColumnInfo("TenVatTu"));
            luMaVatTu.DisplayMember = "TenVatTu";
            luMaVatTu.ValueMember = "MaVatTu";
            luMaVatTu.NullText = string.Empty;
            luMaVatTu.Columns["IDVatTu"].Visible = false;

            //delete
            gcXoa.Width = 20;
            gcXoa.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            //Format columns
            gcM.DisplayFormat.FormatType = FormatType.Numeric;
            gcM.DisplayFormat.FormatString = "{0:0,0.00}";
            gcS.DisplayFormat.FormatType = FormatType.Numeric;
            gcS.DisplayFormat.FormatString = "{0:0,0.00}";
            gcL.DisplayFormat.FormatType = FormatType.Numeric;
            gcL.DisplayFormat.FormatString = "{0:0,0.00}";
            gcXL.DisplayFormat.FormatType = FormatType.Numeric;
            gcXL.DisplayFormat.FormatString = "{0:0,0.00}";
            gcDMTT.DisplayFormat.FormatType = FormatType.Numeric;
            gcDMTT.DisplayFormat.FormatString = "{0:0,0.00}";
            gcTongCan.DisplayFormat.FormatType = FormatType.Numeric;
            gcTongCan.DisplayFormat.FormatString = "{0:0,0.00}";
            gcSLCan.DisplayFormat.FormatType = FormatType.Numeric;
            gcSLCan.DisplayFormat.FormatString = "{0:0,0.00}";

            gcTongCan.OptionsColumn.AllowEdit = false;

            gcMaVatTu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcMoTa.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcS.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcM.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcXL.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcTongCan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDMTT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcSLCan.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcDauTam.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
            gcGhiChu.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            gcS.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcM.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcXL.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcTongCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcDMTT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
            gcSLCan.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;

            //Chung tu
            if (MaID == "0")
            {
                txtChungtu.Text = "LSX" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year
                + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second;
            }
        }

        public void LoadGrid()
        {
            var dataColumns = new DataTable();
            dataColumns.Columns.Add("Xóa");
            dataColumns.Columns.Add("Mã vật tư");
            dataColumns.Columns.Add("Mô tả");
            dataColumns.Columns.Add("S", typeof(double));
            dataColumns.Columns.Add("M", typeof(double));
            dataColumns.Columns.Add("L", typeof(double));
            dataColumns.Columns.Add("XL", typeof(double));
            dataColumns.Columns.Add("Tổng cần", typeof(double));
            dataColumns.Columns.Add("ĐMTT", typeof(double));
            dataColumns.Columns.Add("SL cần", typeof(double));
            dataColumns.Columns.Add("Đầu tấm");
            dataColumns.Columns.Add("Ghi chú");

            for (int i = 0; i < 1; i++)
            {
                dataColumns.Rows.Add("");
            }
            gridMayKhoan.DataSource = null;
            gridMayKhoan.DataSource = dataColumns;
        }

        public void LoadImage()
        {
            var data = DataAll.SelectAll("DkChung_HinhAnh", "*", " WHERE MaAnh='" + txtMaSP.Text.Trim() + "'");
            if (data.Rows.Count > 0)
            {
                var content = (byte[])data.Rows[0]["Anh"];
                if (content != null)
                {
                    Image newImage;
                    using (var ms = new MemoryStream(content, 0, content.Length))
                    {
                        ms.Write(content, 0, content.Length);
                        newImage = Image.FromStream(ms, true);
                    }
                    pbAnh.Image = newImage;
                }
                else
                {
                    pbAnh.Image = null;
                }
            }
            else
            {
                MessageBox.Show("Không tìm thấy mã ảnh", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void bntLuLai_Click(object sender, EventArgs e)
        {
            if (MaID != "0")
            {
                if (gvMayKhoan.RowCount > 0)
                {
                    string message = string.Empty;
                    message = DataAll.Update("[GiaCong]", "SoLuong=" + txtSoLuongMayKhoan.Text + ",GhiChu=N'" + txtGhiChu.Text + "' WHERE ID =" + MaID);
                    if (message == "success")
                    {
                        var dataview = (DataView)gvMayKhoan.DataSource;
                        for (int i = 0; i < gvMayKhoan.RowCount; i++)
                        {
                            var mahang = (DataRowView)luMaVatTu.GetDataSourceRowByKeyValue(gcMaVatTu.View.GetRowCellValue(i, "Mã vật tư").ToString());
                            message = DataAll.Update("GiaCong_ChiTiet", "IDVatTu=N'" + mahang.Row.ItemArray[0]
                                + "',S=N'" + gcS.View.GetRowCellValue(i, "S") + "',M=N'" + gcS.View.GetRowCellValue(i, "M")
                                + "',L=N'" + gcS.View.GetRowCellValue(i, "L") + "',XL=N'" + gcS.View.GetRowCellValue(i, "XL")
                                + "',DMTT=N'" + gcS.View.GetRowCellValue(i, "ĐMTT")
                                + "',TongCan=N'" + gcS.View.GetRowCellValue(i, "Tổng cần") + "',SoLuongCan=N'" + gcS.View.GetRowCellValue(i, "SL cần")
                                + "',DauTam=N'" + gcS.View.GetRowCellValue(i, "Đầu tấm") + "',MoTa=N'" + gcS.View.GetRowCellValue(i, "Mô tả")
                                + "',GhiChu=N'" + gcS.View.GetRowCellValue(i, "Ghi chú") + "' WHERE ID =" + dataview[i]["ID"]);
                            if (message != "success")
                            {
                                break;
                            }
                        }
                    }
                    MessageBox.Show(message.Equals("success") ? "Sửa may khoán thành công" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    Close();
                }
            }
            else
            {
                if (gvMayKhoan.RowCount > 0)
                {
                    string message = string.Empty;
                    var data = DataAll.SelectCustomer("INSERT INTO [GiaCong] VALUES('" + IdSanPham + "','"
                        + dtNgayLapLenh.Value.ToString("MM/dd/yyyy") + "','"
                        + Main.UserID + "',"
                        + txtSoLuongMayKhoan.Text + ",'" + DateTime.Now.ToString("MM/dd/yyyy") + "','"
                        + DateTime.Now.ToString("MM/dd/yyyy") + "'," + Convert.ToInt32(TrangThai.MayKhoan_TaoMoi) + ",'"
                        + dtNgayChungTu.Value.ToString("MM/dd/yyyy") + "','"
                        + dtNgayHachToan.Value.ToString("MM/dd/yyyy") + "','"
                        + txtChungtu.Text + "','"
                        + txtGhiChu.Text + "',N'"
                        + "Phòng cắt" + "',N'"
                        + "Phòng KCS" + "','"
                        + Convert.ToDateTime(dtSXTuNgay.Value).ToString("MM/dd/yyyy") + "','"
                        + Convert.ToDateTime(dtSXDenNgay.Value).ToString("MM/dd/yyyy") + "') SELECT MAX(ID) FROM GiaCong");
                    for (int i = 0; i < gvMayKhoan.RowCount; i++)
                    {
                        var id_vattu = (DataRowView)luMaVatTu.GetDataSourceRowByKeyValue(gcMaVatTu.View.GetRowCellValue(i, "Mã vật tư").ToString());
                        message = DataAll.InsertAll("GiaCong_ChiTiet", data.Rows[0][0] + "," + id_vattu.Row.ItemArray[0]
                            + "," + gcS.View.GetRowCellValue(i, "S")
                            + "," + gcM.View.GetRowCellValue(i, "M")
                            + "," + gcL.View.GetRowCellValue(i, "L")
                            + "," + gcXL.View.GetRowCellValue(i, "XL")
                            + "," + 0
                            + "," + gcDMTT.View.GetRowCellValue(i, "ĐMTT")
                            + "," + gcTongCan.View.GetRowCellValue(i, "Tổng cần")
                            + "," + gcSLCan.View.GetRowCellValue(i, "SL cần")
                            + "," + gcDauTam.View.GetRowCellValue(i, "Đầu tấm")
                            + "," + gcMoTa.View.GetRowCellValue(i, "Mô tả")
                            + "," + gcGhiChu.View.GetRowCellValue(i, "Ghi chú"));
                        if (message != "success")
                        {
                            DataAll.Delete("GiaCong_ChiTiet", "WHERE IDGiaCong=" + data.Rows[0][0]);
                            break;
                        }
                    }
                    message = DataAll.Update("Cat", "TrangThai = " + Convert.ToInt32(TrangThai.Cat_MayKhoanDaCapNhat) + " WHERE ID = " + IdCat);
                    MessageBox.Show(message.Equals("success") ? "Cập nhật cắt thành công!" : message, "Message",
                                    MessageBoxButtons.OK,
                                    message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                    Close();
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void gvMayKhoan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.Caption == "S" && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["S"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["M"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["L"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["XL"]).ToString()))
            {
                gvMayKhoan.SetRowCellValue(e.RowHandle, gvMayKhoan.Columns["Tổng cần"], Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "Số lượng"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "S"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "M"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "L"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "XL")));
            }
            if (e.Column.Caption == "M" && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["S"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["M"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["L"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["XL"]).ToString()))
            {
                gvMayKhoan.SetRowCellValue(e.RowHandle, gvMayKhoan.Columns["Tổng cần"], Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "Số lượng"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "S"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "M"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "L"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "XL")));
            }

            if (e.Column.Caption == "L" && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["S"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["M"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["L"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["XL"]).ToString()))
            {
                gvMayKhoan.SetRowCellValue(e.RowHandle, gvMayKhoan.Columns["Tổng cần"], Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "Số lượng"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "S"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "M"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "L"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "XL")));
            }

            if (e.Column.Caption == "XL" && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["S"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["M"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["L"]).ToString())
           && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["XL"]).ToString()))
            {
                gvMayKhoan.SetRowCellValue(e.RowHandle, gvMayKhoan.Columns["Tổng cần"], Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "Số lượng"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "S"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "M"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "L"))
                    + Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "XL")));
            }
            if (e.Column.Caption == "ĐMTT" && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["Tổng cần"]).ToString())
                && !string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(e.RowHandle, gvMayKhoan.Columns["ĐMTT"]).ToString()))
            {
                gvMayKhoan.SetRowCellValue(e.RowHandle, gvMayKhoan.Columns["SL cần"], Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "Tổng cần")) * Convert.ToDouble(gvMayKhoan.GetRowCellValue(e.RowHandle, "ĐMTT")));
            }
        }

        private void buttonDelete_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            if (gvMayKhoan.FocusedRowHandle < gvMayKhoan.RowCount - 1)
            {
                DialogResult dialogResult = MessageBox.Show("Bạn có chắc chắn muốn xóa.", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.Yes)
                {
                    var dataview = (DataView)gvMayKhoan.DataSource;
                    if (MaID != "0")
                    {
                        if (!string.IsNullOrEmpty(gvMayKhoan.GetRowCellValue(gvMayKhoan.FocusedRowHandle, "ID").ToString()))
                        {

                            string message = DataAll.Delete("GiaCong_ChiTiet", " ID = " + dataview[gvMayKhoan.FocusedRowHandle]["ID"]);
                            MessageBox.Show(message.Equals("success") ? "Xóa may khoán thành công" : message, "Message",
                                                MessageBoxButtons.OK,
                                                message.Equals("success") ? MessageBoxIcon.Information : MessageBoxIcon.Error);
                        }
                    }
                    (gridMayKhoan.MainView as GridView).DeleteSelectedRows();
                }
            }
        }

        private void pbAnh_Click(object sender, EventArgs e)
        {
            XemAnh.MaID = txtMaSP.Text;
            var anh = new XemAnh();
            anh.ShowDialog();
            XemAnh.MaID = "0";
        }

        private void btnPhieuYeuCau_Click(object sender, EventArgs e)
        {
            MayKhoan_PhieuYeuCau_ThemMoi.IdCat = IdCat;
            MayKhoan_PhieuYeuCau_ThemMoi.IdSanPham = IdSanPham;
            var lss = new MayKhoan_PhieuYeuCau_ThemMoi();
            lss.ShowDialog();
            MayKhoan_PhieuYeuCau_ThemMoi.IdCat = "0";
            MayKhoan_PhieuYeuCau_ThemMoi.IdSanPham = "0";
            DataTable dt = DataAll.SelectAll("PhieuYeuCau", "ID", "WHERE IdSanPham =" + IdSanPham + " AND PhongBan = " + Convert.ToInt32(PhongBan.PhongMayKhoan));
            if (dt.Rows.Count > 0)
            {
                Close();
            }
        }
    }
}
